// 搜索货币面额对应的名称

#include <stdio.h>

// 使用两个数组
// 单位：美分
int amount[] = {1, 5, 10, 25, 50};
char *name[] = {"penny", "nickel", "dime", "quarter", "half-dollar"};

// 使用结构体，将面额和值放在一起，对cache更友好
struct
{
    int amount;
    char *name;
} coins[] = {
    {1, "penny"},
    {5, "nickel"},
    {10, "dime"},
    {25, "quarter"},
    {50, "half-dollar"},
}

int search(int key, int a[], int len);

int main(int argc, char const *argv[])
{
    int k = 10;
    // int r = search(k, amount, sizeof(amount) / sizeof(amount[0]));
    for (int i = 0; i < sizeof(coins) / sizeof(coins[0]); i++)
    {
        if (k == coins[i].amount)
        {
            printf("%s\n", coins.name);
        }
    }
    // if (r > -1)
    // {
    //     printf("%s\n", name[r]);
    // }
    printf("%d\n", r);
    
    return 0;
}

int search(int key, int a[], int len)
{
    int ret = -1;
    for (int i = 0; i < len; i++)
    {
        if (key == a[i])
        {
            ret = i;
            break;
        }
    }
    // 单一出口
    return ret;
}